# agents/factor_agents/castorice_agent.py
from .base_factor_agent import BaseFactorAgent
from game_engine.game_state import FactorDevelopment, GameWorld
import numpy as np

class CastoriceAgent(BaseFactorAgent):
    """遐蝶智能体 - 烧血爆发特化 [火种：死亡]"""
    
    def __init__(self, factor: FactorDevelopment, game_world: GameWorld):
        super().__init__(factor, game_world)
        self.personality_traits.update({
            "aggression": 0.9,      # 极高攻击性
            "caution": 0.2,         # 极低谨慎性
            "death_embrace": 0.95,  # 拥抱死亡
            "sacrifice_willingness": 0.8  # 牺牲意愿
        })
        self.blood_burn_stacks = 0
        self.last_sacrifice_time = 0
    
    def make_decision(self, current_time: int) -> str:
        """遐蝶的决策逻辑"""
        situation = self.evaluate_situation()
        
        # 死亡绽放状态
        if self.blood_burn_stacks >= 3:
            if situation["nearby_threats"]:
                target = situation["nearby_threats"][0]
                return f"death_bloom:{target.name}"
            else:
                return "seek_sacrifice"
        
        # 积累血燃层数
        if self.factor.attributes['health'] > self.factor.attributes['health'] * 0.4:
            if situation["nearby_threats"]:
                return "burn_blood_attack"
            else:
                return "self_sacrifice"
        
        # 濒死爆发
        if self.factor.attributes['health'] < self.factor.attributes['health'] * 0.2:
            if self.should_use_special_ability():
                return "final_dance"
            else:
                return "desperate_attack"
        
        return "train_blood_magic"
    
    def evaluate_situation(self) -> Dict:
        """评估当前局势"""
        nearby_factors = self.get_nearby_factors()
        threats = [f for f in nearby_factors if self._is_potential_threat(f)]
        
        return {
            "nearby_threats": threats,
            "blood_burn_stacks": self.blood_burn_stacks,
            "health_percentage": self.factor.attributes['health'] / 100.0,
            "death_ready": self.blood_burn_stacks >= 3 or self.factor.attributes['health'] < 20
        }
    
    def apply_blood_burn(self, amount: int = 1):
        """应用血燃效果"""
        self.blood_burn_stacks += amount
        # 每层血燃消耗生命值
        burn_damage = 5 * amount
        self.factor.attributes['health'] = max(1, self.factor.attributes['health'] - burn_damage)